package tools

object Str {
    private fun Int.toHexString(): String = Integer.toHexString(this)

    //char ->unicode
    private fun encode(char: Char) = "\\u${char.code.toHexString()}"

    //String ->unicode
    fun encode(text: String) = text
        .toCharArray().joinToString(separator = "", truncated = "") { encode(it) }

    //unicode ->String
    fun decode(encodeText: String): String {
        fun decode1(unicode: String) = unicode.toInt(16).toChar()
        val unicodes = encodeText.split("\\u").mapNotNull { if (it.isNotBlank()) decode1(it) else null }
        return String(unicodes.toCharArray())
    }
}